<?php

namespace App\Services\User;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Models\User\Address;
use App\Services\BaseServices;
use App\Services\Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class AddressServices extends BaseServices
{

    /**
     * 获取地址列表
     * @param  int  $userId
     * @return \App\Models\User\Address[]|Collection
     */
    public function getAddressListByUserId(int $userId)
    {
        return Address::query()
            ->where('user_id', $userId)
            ->get();
    }

    /**
     * 获取用户地址
     * @param $userId
     * @param $addressId
     * @return \App\Models\User\Address|Model|null
     */
    public function getAddress($userId, int $addressId)
    {
        return Address::query()->where('user_id', $userId)->where('id', $addressId)
            ->first();
    }

    /**
     * 删除地址
     * @param $userId
     * @param $addressId
     * @return bool|null
     * @throws BusinessException
     * @throws Exception
     */
    public function delete($userId, $addressId)
    {
        $address = $this->getAddress($userId, $addressId);
        if (empty($address)) {
            $this->throwBusinessException(CodeResponse::PARAM_ILLEGAL);
        }
        return $address->delete;
    }

//    public function detail($userId, $addressId)
//    {
//        $address = $this->getAddress($userId, $addressId);
//        if (is_null($address)) {
//            $this->throwBusinessException(CodeResponse::PARAM_ILLEGAL);
//        }
//
//        return $address->get();
//    }
    public function addAddress($userId, $addressInfo)
    {
        $addressInfo['user_id'] = $userId;
        $addressInfo['add_time'] = date('Y-m-d H:i:s');
        Address::query()->insert($addressInfo);
        return true;
    }

    public function editAddress($userId, $addressId, $addressInfo)
    {
        Address::query()->where('user_id', $userId)->where('id', $addressId)->update($addressInfo);
        return true;
    }

    /**
     * 校验参数
     * 'name','tel','province','city','count','area_code','address_detail','is_default';
     * @param $address
     * @return bool
     * @throws BusinessException
     */
    public function checkAddressParam($address)
    {
        // 姓名
        if (empty($address['name'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：姓名');
        }
        // 测试收货手机号码是否正确
        if (empty($address['tel'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：电话');
        }
        // 正则校验手机号 暂时不写
        $mobileRegular = '/^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\d{8}$/';
        if (empty(preg_match($mobileRegular, $address['tel']))) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：电话');
        }
        // 省/直辖市
        if (empty($address['province'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：省/直辖市');
        }
        // 市
        if (empty($address['city'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：市');
        }
        // 县区
        if (empty($address['county'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：县区');
        }
        // 邮编
        if (empty($address['area_code'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：邮编');
        }
        // 详细地址
        if (empty($address['address_detail'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：详细地址');
        }
        // 是否设为默认地址
        if (is_null($address['is_default'])) {
            throw new BusinessException(CodeResponse::PARAM_ILLEGAL, '参数错误：默认地址');
        }
        return true;
    }
}
